package com.eloo.otsclient.ui;

import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
 
 
public abstract class QRDialog extends JDialog implements ActionListener,PropertyChangeListener {
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel QRcode; 
	private	 JTextField qrCodeValue;  
	private JOptionPane optionPane;
	private JButton okButton = null; 
	private String btnString1 = "要";
	private String btnString2 = "不要";
	private boolean isContinue=false;
	
	private String qrString;
 
	

	public QRDialog(Frame aFrame, String magicWord,String msg,Icon qr) {
		super(aFrame, true);  
	 	setTitle(magicWord); 
		QRcode = new  JLabel(qr);
		qrCodeValue=new JTextField(20); 
		Object[] array = { msg, qrCodeValue,QRcode}; 
		Object[] options = { btnString1, btnString2 };
 
		optionPane = new JOptionPane(array, JOptionPane.QUESTION_MESSAGE,
				JOptionPane.YES_NO_OPTION, null, options, options[0]);
 
		setContentPane(optionPane);

		// Handle window closing correctly.
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) { 
				optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
			}
		});
 
		addComponentListener(new ComponentAdapter() {
			public void componentShown(ComponentEvent ce) {
				qrCodeValue.requestFocusInWindow();
			}
		});
 
		qrCodeValue.addActionListener(this); 
		optionPane.addPropertyChangeListener(this);
		pack();
		setLocation(aFrame.getLocation().x+10,aFrame.getLocation().y+10);
		toFront();
		setVisible(true);
	
	}
 
	public void actionPerformed(ActionEvent e) { 
		if(e.getSource()!=null &&  e.getSource()==okButton)
		optionPane.setValue(((JButton)e.getSource()).getText());
	}
 
	public void propertyChange(PropertyChangeEvent e) {
		String prop = e.getPropertyName();

		if (isVisible()&& (e.getSource() == optionPane)&& (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY
						.equals(prop))) {
			Object value = optionPane.getValue(); 
			if (value == JOptionPane.UNINITIALIZED_VALUE) { 
				return;
			}  
			if (btnString1.equals(value)) { 
				qrString=qrCodeValue.getText().trim();
				afterOK(qrString);
			} else { 
				afterCancel(); 
			}
			clearAndHide();
		}
	} 
	public void clearAndHide() {
	 
		setVisible(false);
	}
	public String getQrString() {
		return qrString;
	}

	public void setQrString(String qrString) {
		this.qrString = qrString;
	}
	public boolean isContinue() {
		return isContinue;
	}

	public void setContinue(boolean isContinue) {
		this.isContinue = isContinue;
	}
	public abstract void afterOK(String qrString);
	public abstract void afterCancel();
}
